package hotLeetcode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class H763Leetcode {
    public List<Integer> partitionLabels(String s) {


        List<Integer> ans = new ArrayList<>();
        char[] charArray = s.toCharArray();

        //找出每个字母最后出现的下标
        Map<Character, Integer> lastIndexMap = new HashMap<>();
        for (int i = 0; i < charArray.length; i++) {
            lastIndexMap.put(charArray[i], i);
        }

        int start = 0, end = 0;

        for (int i = 0; i < charArray.length; i++) {
            end = Math.max(end, lastIndexMap.get(charArray[i]));
            if (end == i) {
                ans.add(end - start + 1);
                start = end + 1;
            }
        }


        return ans;

    }
}
